package com.huixue.cn.ztej.weixin.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.huixue.cn.ztej.constants.Constant;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;
import com.huixue.cn.ztej.weixin.bean.OAuthInfo;
import com.huixue.cn.ztej.weixin.bean.XiaoOAuthInfo;
import com.huixue.cn.ztej.weixin.push.PushItem;
import com.huixue.cn.ztej.weixin.push.PushMessage;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author Administrator
 *
 */
public class WeixinHelper {
	public static String getUserOpenId(String accessToken){  
        String result = null;  
        String requestUrl ="https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken;  
        JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);  
        if(null != jsonObject){  
            result = jsonObject.get("data")+"";  
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("openid");
            if(jsonArray!=null && jsonArray.size()>0){
            	result = jsonArray.getString(0);
            }
            	
        }  
        return result;  
    } 
	
	
	public static OAuthInfo getUserOpenId(HttpServletRequest request){  
		String code = request.getParameter("code");
		OAuthInfo  aouAuthInfo = null;
		if(code!=null){
			 aouAuthInfo = WeixinUtil.getOAuthOpenId(Constant.AppID, Constant.AppSecret, code,request);
			 request.getSession().setAttribute("aouAuthInfo", aouAuthInfo);
		}else if(request.getSession().getAttribute("aouAuthInfo")!=null){
			aouAuthInfo = (OAuthInfo)(request.getSession().getAttribute("aouAuthInfo"));
		}else{
			aouAuthInfo = new OAuthInfo();
			aouAuthInfo.setOpenId("o4UWRwOwW8q7bOv5NYFJkAB7ayUY");
		}
		return aouAuthInfo;
	}
	
	public static XiaoOAuthInfo getXiaoChengXuUserOpenId(HttpServletRequest request){
		String code = request.getParameter("code");
		XiaoOAuthInfo  aouAuthInfo = null;
		if(code!=null){
			aouAuthInfo = WeixinUtil.getXiaochengxuOAuthOpenId(Constant.AppID, Constant.AppSecret, code,request);
		}else{
			aouAuthInfo = new XiaoOAuthInfo();
			aouAuthInfo.setOpenid("");
		}
		return aouAuthInfo;
	}
	
	/**
	 * 消息推送
	 * @param json
	 */
	public static JSONObject pushMessage(PushMessage msg){
		
		String accessToken = WeixinUtil.getAccessToken(Constant.AppID, Constant.AppSecret);
		/*msg = new PushMessage();
		msg.setTouser("oB-oR0UE8BgWSgWtuVNDGRxQKbSY");
		msg.setTemplate_id("-qLUi5ywgd3Fqe34OqlUXdKM4Fc7X-n9_KBw_yebjew");
		msg.setForm_id("FORMID");
		
		
		Map<String,PushItem> map = new HashMap<String,PushItem>();
		PushItem pushItem1 = new PushItem("12233344", "#173177");
		map.put("单号", pushItem1);
		
		PushItem pushItem2 = new PushItem("12233344", "#173177");
		map.put("金额", pushItem2);
		
		PushItem pushItem3 = new PushItem("12233344", "#173177");
		map.put("下单时间", pushItem3);
		
		PushItem pushItem4 = new PushItem("12233344", "#173177");
		map.put("物品名称", pushItem4);
		msg.setData(map);*/
		String json = new JacksonUtil().getJson(msg);
		//JSONObject result = WeixinUtil.pushMessage(json, "wbYkViWWIOadip7rk_lNHO4YKciiry7Xy8A7rLE5LbNlPEGDfv55mgUsmC1aQD7xKU3zzU3vz2m05sgIBJsYAQzpKTvq4KUaElBsOEkmf1XaCaBId1_WqvWjFJiekeglFVKbAAAZPE");
		JSONObject result = WeixinUtil.pushMessage(json,accessToken);
		return result;
	}
	
	
	public static void main(String[] args) {
		//String accessToken = WeixinUtil.getAccessToken(Constant.AppID, Constant.AppSecret);
		//System.out.println("========accessToken====="+accessToken);
		PushMessage msg = new PushMessage();
		msg.setTouser("oB-oR0UE8BgWSgWtuVNDGRxQKbSY");
		//推送模板
		msg.setTemplate_id("-qLUi5ywgd3Fqe34OqlUXdKM4Fc7X-n9_KBw_yebjew");
		//formid--表单生成
		msg.setForm_id("1510222810354");
		Map<String,PushItem> map = new HashMap<String,PushItem>();
		PushItem pushItem1 = new PushItem("12233344", "#3A3A3A");
		map.put("keyword1", pushItem1);
		
		PushItem pushItem2 = new PushItem("30元", "#3A3A3A");
		map.put("keyword2", pushItem2);
		
		PushItem pushItem3 = new PushItem("2015年01月05日", "#3A3A3A");
		map.put("keyword3", pushItem3);
		
		PushItem pushItem4 = new PushItem("粤海喜来登酒店", "#3A3A3A");
		map.put("keyword4", pushItem4);
		msg.setData(map);
		String json = new JacksonUtil().getJson(msg);
		System.out.println(json);
		
		JSONObject result = pushMessage(msg);
		System.out.println(result.toString());
		
	}
}
